<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE Client</title>
</head>
<body>
    <h1>SSE Client</h1>
    <input type="text" id="userId" placeholder="Enter user ID">
    <button onclick="connectSSE()">Connect</button>
    <div id="messages"></div>

    <script>
        let eventSource;

        function connectSSE() {
            const userId = document.getElementById('userId').value;
            if (!userId) {
                alert('Please enter a user ID');
                return;
            }

            if (eventSource) {
                eventSource.close();
            }

            const url = `http://127.0.0.1:18080/sse/connect?userId=${userId}`;
            eventSource = new EventSource(url);

            eventSource.onopen = function() {
                console.log('Connection opened');
            };

            eventSource.onmessage = function(event) {
                const messagesDiv = document.getElementById('messages');
                messagesDiv.innerHTML += `<p>${event.data}</p>`;
            };

            eventSource.onerror = function(error) {
                console.error('EventSource failed:', error);
                eventSource.close();
            };
        }
    </script>
</body>
</html>